package com.ruin.tech.ahri.controller;

import com.ruin.tech.ahri.model.dto.Article;
import com.ruin.tech.ahri.model.dto.ArticleDTO;
import com.ruin.tech.ahri.model.dto.Comment;
import com.ruin.tech.ahri.model.dto.ArticlePageDTO;
import com.ruin.tech.ahri.model.vo.ArticleVO;
import com.ruin.tech.ahri.model.vo.CommentVO;
import com.ruin.tech.ahri.result.Result;
import com.ruin.tech.ahri.result.ResultCode;
import com.ruin.tech.ahri.result.ResultFactory;
import com.ruin.tech.ahri.service.IAppreciateService;
import com.ruin.tech.ahri.service.IArticleService;
import com.ruin.tech.ahri.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program ahri
 * @description: 文章控制器
 * @author: ruin
 * @create: 2019/12/04 08:14
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    IArticleService articleService;

    @Autowired
    ICommentService commentService;

    @Autowired
    IAppreciateService appreciateService;

    //TODO MultipartException: Current request is not a multipart request 异常
    @PostMapping(value = "/uploadAndEdit", produces = "application/json;charset=utf-8")
    public Result uploadAndEdit(@RequestParam("cImg") MultipartFile cImg, HttpServletRequest request) {
        Long userId = Long.valueOf(request.getParameter("userId"));
        Long id = Long.valueOf(request.getParameter("id"));
        Article article = new Article();
        if(id == -1L){
            article.setId(null);
        }else{
            article.setId(id);
        }
        if(userId != -1){
            article.setUserId(userId);
        }else{
            return ResultFactory.buildFailResult(ResultCode.NO_LOGIN_EXCEPTION);
        }


        article.setTitle(request.getParameter("title"));
        article.setSummary(request.getParameter("summary"));
        article.setIsTop(Integer.valueOf(request.getParameter("isTop")));
        article.setType(Integer.valueOf(request.getParameter("type")));
        article.setCoverImg(request.getParameter("coverImg"));
        article.setContent(request.getParameter("content"));
        article.setCommentCount(Integer.valueOf(request.getParameter("commentCount")==null?"0":request.getParameter("commentCount")));
        article.setLikeCount(Integer.valueOf(request.getParameter("likeCount")==null?"0":request.getParameter("likeCount")));
        article.setReadCount(Integer.valueOf(request.getParameter("readCount")==null?"0":request.getParameter("readCount")));
        try{
            articleService.saveArticle(article,cImg);
        }catch (Exception e){
            log.error("文章发布或编辑失败："+article.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.PUBLISH_ARTICLE_FAIL);
        }

        return ResultFactory.buildSuccessResult(null);
    }

    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    public Result edit(@RequestBody Article article) {

        try{
            articleService.saveArticle(article,null);
        }catch (Exception e){
            log.error("文章发布或编辑失败："+article.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.PUBLISH_ARTICLE_FAIL);
        }

        return ResultFactory.buildSuccessResult(null);
    }



    @PostMapping(value = "/findPage", produces = "application/json;charset=utf-8")
    public Result findArticleByType(@RequestBody ArticlePageDTO articlePageDTO) {
        try{
            List<Article> articleList = articleService.findArticlePage(articlePageDTO);
            return ResultFactory.buildSuccessResult(articleList);
        }catch (Exception e){
            log.error("文章分页查询失败："+ articlePageDTO.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_ARTICLE_FAIL);
        }
    }

    @PostMapping(value = "/findById", produces = "application/json;charset=utf-8")
    public Result findArticleById(@RequestBody ArticleDTO param) {
        try{
            ArticleVO articleVO = articleService.findArticleById(param);
            return ResultFactory.buildSuccessResult(articleVO);
        }catch (Exception e){
            log.error("文章详情查询失败,id=："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_ARTICLE_DETAIL_FAIL);
        }
    }

    @PostMapping(value = "/addArcticleReadCount", produces = "application/json;charset=utf-8")
    public Result addArcticleReadCount(@RequestBody ArticleDTO param) {
        try{
            articleService.addArcticleReadCount(param);
            return ResultFactory.buildSuccessResult(null);
        }catch (Exception e){
            log.error("文章浏览记录+1失败,id=："+param.getId(),e);
            return ResultFactory.buildFailResult(ResultCode.ADD_READ_COUNT_FAIL);
        }
    }

    @PostMapping(value = "/comment/submit", produces = "application/json;charset=utf-8")
    public Result submitComment(@RequestBody Comment param) {
        try{
            CommentVO commentVO = commentService.submitComment(param);
            return ResultFactory.buildSuccessResult(commentVO);
        }catch (Exception e){
            log.error("评论失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.SUBMIT_COMMENT_FAIL);
        }
    }

    @PostMapping(value = "/comment/findCommentsByArticleId", produces = "application/json;charset=utf-8")
    public Result findCommentsByArticleId(@RequestBody ArticleDTO param) {
        try{
            List<CommentVO> commentVOList = commentService.findCommentsByArticleId(param);
            return ResultFactory.buildSuccessResult(commentVOList);
        }catch (Exception e){
            log.error("获取文章评论信息失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_COMMENT_FAIL);
        }
    }

    @PostMapping(value = "/appreciate", produces = "application/json;charset=utf-8")
    public Result appreciate(@RequestBody ArticleDTO param) {
        try{
            appreciateService.appreciate(param);
            return ResultFactory.buildSuccessResult(null);
        }catch (Exception e){
            log.error("点赞失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.APPRECIATE_FAIL);
        }
    }

    @PostMapping(value = "/mylook", produces = "application/json;charset=utf-8")
    public Result mylook(@RequestBody ArticlePageDTO param) {
        try{
            List<Article> articleList = articleService.mylook(param);
            return ResultFactory.buildSuccessResult(articleList);
        }catch (Exception e){
            log.error("获取浏览记录失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_MYLOOK_ARTICLES_FAIL);
        }
    }

    @PostMapping(value = "/mylike", produces = "application/json;charset=utf-8")
    public Result mylike(@RequestBody ArticlePageDTO param) {
        try{
            List<Article> articleList = articleService.mylike(param);
            return ResultFactory.buildSuccessResult(articleList);
        }catch (Exception e){
            log.error("获取收藏列表失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_MYLIKE_ARTICLES_FAIL);
        }
    }

    @PostMapping(value = "/comment/findCommentsByUserId", produces = "application/json;charset=utf-8")
    public Result findCommentsByUserId(@RequestBody ArticleDTO param) {
        try{
            List<CommentVO> commentVOList = commentService.findCommentsByUserId(param);
            return ResultFactory.buildSuccessResult(commentVOList);
        }catch (Exception e){
            log.error("获取用户评论信息失败："+param.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_COMMENT_FAIL);
        }
    }
}
